package com.main.locatme;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.locatme.model.Langue;
import com.locatme.model.Personne;
import com.locatme.modelBD.LangueBD;
import com.locatme.modelBD.UtilisateurBD;

public class CreateActivity extends Activity{
    private Spinner spinner1;
    private UtilisateurBD userBD;
    private LangueBD langueBD;
    private Langue langue;
    public Personne user;
    private String login;
    private String password;
    private String pseudo;



    private  EditText loginEditText ;
    private  EditText passEditText1;
    private  EditText passEditText2;
    private  EditText pseudoEditText;
    private  Spinner spinLangue;


    public Langue getLangue() {
        return langue;
    }

    public void setLangue(Langue langue) {
        this.langue = langue;
    }

    public EditText getPseudoEditText() {
        return pseudoEditText;
    }

    public void setPseudoEditText(EditText pseudoEditText) {
        this.pseudoEditText = pseudoEditText;
    }

    public EditText getPassEditText2() {
        return passEditText2;
    }

    public void setPassEditText2(EditText passEditText2) {
        this.passEditText2 = passEditText2;
    }

    public EditText getPassEditText1() {
        return passEditText1;
    }

    public void setPassEditText1(EditText passEditText1) {
        this.passEditText1 = passEditText1;
    }

    public EditText getLoginEditText() {
        return loginEditText;
    }

    public void setLoginEditText(EditText loginEditText) {
        this.loginEditText = loginEditText;
    }

    public UtilisateurBD getUserBD() {
        return userBD;
    }

    public void setUserBD(UtilisateurBD userBD) {
        this.userBD = userBD;
    }

    public LangueBD getLangueBD() {
        return langueBD;
    }

    public void setLangueBD(LangueBD langueBD) {
        this.langueBD = langueBD;
    }

    public Spinner getSpinLangue() {
        return spinLangue;
    }

    public void setSpinLangue(Spinner spinLangue) {
        this.spinLangue = spinLangue;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create);
        final Button createUserButton = (Button) findViewById(R.id.create_user);
        loginEditText = (EditText) findViewById(R.id.user_email);
        passEditText1 = (EditText) findViewById(R.id.user_password1);
        passEditText2 = (EditText) findViewById(R.id.user_password2);
        pseudoEditText = (EditText) findViewById(R.id.user_pseudo);
        spinLangue = (Spinner) findViewById(R.id.spinnerlanguep);
        login = loginEditText.getText().toString();
        password = passEditText1.getText().toString();
        //Concerne aussi les enseignes
        pseudo = pseudoEditText.getText().toString();

        createUserButton.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {

                if (loginEditText.getText().toString().isEmpty() ||
                        (passEditText1.getText().toString().isEmpty() || passEditText2.getText().toString().isEmpty())) {
                    Toast.makeText(CreateActivity.this, "Please fill all required fields", Toast.LENGTH_LONG).show();
                }else if(!passEditText1.getText().toString().contentEquals(passEditText2.getText().toString()))
                {
                    Toast.makeText(CreateActivity.this, "Password verification does not match", Toast.LENGTH_LONG).show();
                }
                else {
                    inserer(v);
                }
            }

        });

    }//Fin on create

    public void addItemsOnSpinner() {

        spinner1 = (Spinner) findViewById(R.id.spinnerlanguep);
        List<String> list = new ArrayList<String>();
        list.add("Hongrois");
        list.add("Turc");
        list.add("alien");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(dataAdapter);
    }


    public void inserer(View v) {

        userBD = new UtilisateurBD(this);
        langueBD = new LangueBD(this);
        user = new Personne(pseudoEditText.getText().toString(),loginEditText.getText().toString(),passEditText1.getText().toString());

        userBD.open();
        langueBD.open();
        int idLangue = langueBD.recupIdLangue(spinLangue.getSelectedItem().toString());
        userBD.insertPersonne(user);
        langueBD.insererLanguePersonne(loginEditText.getText().toString(), idLangue);
        userBD.close();
        langueBD.close();

        Toast.makeText(this, "Vous venez de vous inscrire en tant que " + loginEditText.getText().toString(), Toast.LENGTH_SHORT).show();
        CreateActivity.this.finish();
    }

}
